#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

class Solution
{
public:
    vector<vector<int>> subsetsWithDup(vector<int> &nums)
    {
        sort(nums.begin(), nums.end());
        vector<vector<int>> res;
        vector<int> v;
        res.push_back(v);
        int right = 1, left = 0, len = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            if (i != 0 && (nums[i] == nums[i - 1]))
                left = res.size() - len;
            else
                left = 0;
            right = res.size();
            len = right - left;
            for (int j = left; j < right; j++)
            {
                v = res[j];
                v.push_back(nums[i]);
                res.push_back(v);
            }
        }
        return res;
    }
};